package com.jit.BluetoothKey.view;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jit.BluetoothKey.R;
import com.jit.BluetoothKey.utils.SharedPreferencesUtil;
import com.jit.BluetoothKey.utils.ToastUtils;
import com.jit.BluetoothKey.utils.UserFiled;


/**
 * 自定义dialog
 */
public class MyDialog implements View.OnClickListener {

    private Context context;
    private  Dialog dialog;
    private  PinCodeChangeListner pinCodeChangeListner;
    private TextView pass;
    private TextView ensure;
    private EditText pinCodeEd;


    public MyDialog(Context context) {
        this.context = context;
    }

    public void setPinCodeChangeListner(PinCodeChangeListner pinCodeChangeListner){
        this.pinCodeChangeListner = pinCodeChangeListner;
    }


    /**
     * 初始化dialog
     * @return
     */
    public Dialog init(){
        dialog = new Dialog (context, R.style.MyDialog);//dialog样式
        LayoutInflater inflater = LayoutInflater.from(context);
        View rootView = inflater.inflate(R.layout.layout_accout_dialog, null);
        dialog.setContentView(rootView);//dialog布局文件
        dialog.setCanceledOnTouchOutside(false);//点击外部不允许关闭dialog

        pass = rootView.findViewById(R.id.pin_code_dialog_pass_tv);
        ensure = rootView.findViewById(R.id.pin_code_dialog_ensure_tv);
        pinCodeEd = rootView.findViewById(R.id.pin_code_dialog_ed);
        pass.setOnClickListener(this);
        ensure.setOnClickListener(this);

        return dialog;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pin_code_dialog_pass_tv:
                // TODO: 2018/2/28  取消输入验证码
                dialog.dismiss();
                break;
            case R.id.pin_code_dialog_ensure_tv:
                // TODO: 2018/2/28   输入验证码
                if (!pinCodeEd.getText().toString().equals("")) {
                    SharedPreferencesUtil.setString(context, UserFiled.SK,pinCodeEd.getText().toString());
                    pinCodeChangeListner.pinCodeChange();
                    dialog.dismiss();
                }else {
                    ToastUtils.showToast(context,"注册码不能为空");
                }
                break;
        }
    }


    public interface PinCodeChangeListner{
        void pinCodeChange();
    }
}
